package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户控制器
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    private static final String CURRENT_USER = "CURRENT_USER";
    @DubboReference
    private UserService userService;

    /**
     * @Author: liangWeiQUQ
     * @Date: 11:41
     * @Parms
     * @ReturnType:
     * @Description:
     */
    //@PostMapping("/login")
    //public Result login(HttpServletRequest request, @RequestBody LoginParam param) {
    //    ////1.输出日志
    //    //log.info("{登录信息}=====>", param);
    //    ////2.调用service查询用户的信息
    //    //User user = userService.findByUsername(param.getUsername());
    //    ////3.进行登录的判断
    //    //if (user == null || !user.getPassword().equals(param.getPassword())) {
    //    //    log.info("{登录失败}，user：",param.getUsername());
    //    //    return new Result(false, MessageConst.LOGIN_FAIL);
    //    //}
    //    //log.info("登录成功，user:", user.getUsername());
    //
    //    return new Result(true, MessageConst.LOGIN_SUCCESS);
    //}
    /**
       * @Author: 传闻中的梁大侠
       * @Date: 15:20 2021/3/8
       * @Parms []
       * @ReturnType: com.itheima.health.entity.Result
       * @Description: 登录成功后的请求地址
     */

    @PostMapping("loginSuccess")
    public Result loginSuccess() {
        return new Result(true, MessageConst.LOGIN_SUCCESS);
    }

    /**
     * @Author: 传闻中的梁大侠
     * @Date: 15:20 2021/3/8
     * @Parms []
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 登录失败后的请求地址
     */
    @PostMapping("loginFail")
    public Result loginFail() {

        return new Result(true, MessageConst.LOGIN_FAIL);
    }

    @GetMapping("getUserName")
    public Result getUserName(){
        Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(null != obj && obj instanceof User){
            //注意此处的user是security包下面的，不是我们的pojo下面的实体
            User user = (User) obj;
            String username = user.getUsername();
            return new Result(true, MessageConst.GET_USERNAME_SUCCESS, username);
        }
        return new Result(false, MessageConst.GET_USERNAME_FAIL);
    }
}
